var p1 = Promise.resolve('aaa');
var p2 = new Promise((resolve, reject) => {
	resolve(p1)
})

var p3 =  new Promise((resolve, reject) => {
	setTimeout(() => {
		resolve(p2)
	})
})

var p4 = new Promise((resolve, reject) => {
	resolve(p3)
}) 

p3.then(value => {
	console.log('value => ', value)
	return p2
}, reason => {
	console.warn('reason => ', reason)
}).then(value => {
	console.log('value2 => ', value)
}, reason => {
	console.warn('reason2 => ', reason)
});


Promise.resolve(Promise.resolve(Promise.resolve(p4))).then(value => {
	console.log('-----------------------------------------\n')
	
	/*
	if(value instanceof Promise) {
		console.log(value)
		value.then(v => {
			console.log('v => ', v)
		}, r={ })
	} else {
		console.log('Promise.resolve(p4) value => ', value)
	}
	*/

	flatPromise(value)

}, reason => {
	console.warn('Promise.resolve(p4) reason => ', reason)
}).catch(reason => {
	console.warn('catch', reason)
	
})


function flatPromise(p, i = 0) {

	if(!(p instanceof Promise)) {
		console.log(" p = ", p)
	} else {
		p.then(value => {
			console.log(`${i} value => `, value)
			flatPromise(value, ++i)
		}, reason =>{ 
			console.warn(`${i} reason => `, reason)
			flatPromise(reason, ++i)
		})
	}
}